package com.share.tool.mock.web;

import com.alibaba.fastjson.JSONObject;
import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.common.utils.DbUtil;
import com.share.common.utils.MinioUtil;
import com.share.tool.mock.model.vo.LogPageListReq;
import com.share.tool.mock.model.vo.VariablePageListReq;
import com.share.tool.mock.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api("通用程序")
@ResponseBody
@RestController
public class CommonController {
    @Autowired
    private HttpVariableService mockHttpVariableService;

    @Autowired
    private VariableService mockVariableService;

    @Autowired
    private ISourceDataService sourceDataService;

    @Autowired
    private ISourceConfService sourceConfService;

    @Autowired
    private DataLogService dataLogService;

    @Autowired
    private DataSource dataSource;

    @ApiOperation("清理删除表中的数据,保留最近的n条")
    @ResponseBody
    @RequestMapping(value = "/common/deleteLog")
    public Result deleteLog(Integer retainNum) throws SQLException {
        final String[] tables = {"mc_data_log", "trig_source_data"};
        Map<String, Object> reMap = DbUtil.deleteDataById(dataSource, tables, retainNum);
        return Result.success(reMap);
    }


    @ApiOperation("清理minio中的文件")
    @ResponseBody
    @RequestMapping(value = "/common/deleteMinioFile")
    public Result deleteMinioFile(Integer retainNum) {
        int delCount = 0;
        if (retainNum == null || retainNum <= 0) {
            retainNum = 0;
        }

        //第一部分
        List<String> list = MinioUtil.listFilesByPrefix("/image/202", true);//按照前缀查找
        if (!CollectionUtils.isEmpty(list)) {
            int size = list.size();
            if (retainNum == 0) {
                delCount = MinioUtil.deleteFiles(list);
            } else if (retainNum < size) {
                List<String> list1 = list.stream().sorted().collect(Collectors.toList());
                List<String> list2 = list1.subList(0, size - retainNum);
                delCount = MinioUtil.deleteFiles(list2);
            }
        }

        //第二部分
        list = MinioUtil.listFilesByPrefix("/pdf/202", true);//按照前缀查找
        if (!CollectionUtils.isEmpty(list)) {
            int size = list.size();
            if (retainNum == 0) {
                delCount += MinioUtil.deleteFiles(list);
            } else if (retainNum < size) {
                List<String> list1 = list.stream().sorted().collect(Collectors.toList());
                List<String> list2 = list1.subList(0, size - retainNum);
                delCount += MinioUtil.deleteFiles(list2);
            }
        }
        return Result.success(delCount);
    }

    //分页查询
    @ResponseBody
    @RequestMapping(value = "/variable/pageList")
    public Result variablePageList(VariablePageListReq req) {
        Page page = mockVariableService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/log/pageList")
    public Result logPageList(LogPageListReq req) {
        Page page = dataLogService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("重新加载 cache数据")
    @RequestMapping(value = "/reload/conf")
    public Result reloadConf() {
        Object variable = mockVariableService.init();//注意顺序

        Object httpVariable = mockHttpVariableService.init();

        Object sourceConf = sourceConfService.init();

        JSONObject obj = new JSONObject();
        obj.put("variable", variable);
        obj.put("httpVariable", httpVariable);
        obj.put("sourceConf", sourceConf);
        return Result.success(obj);
    }

    @ApiOperation("生成数据-并插入sourceData中")
    @RequestMapping(value = "/producer/sourceData")
    public Result producerSourceData() {
        Object o = sourceDataService.producerSourceData();
        return Result.success(o);
    }

    @ApiOperation("消费sourceData表中的数据")
    @RequestMapping(value = "/consumer/sourceData")
    public Result consumerSourceData() {
        Object o = sourceDataService.consumerSourceData();
        return Result.success(o);
    }
}
